#!/bin/bash


version=$1
major=`echo $version|cut -d. -f1`
minor=`echo $version|cut -d. -f2`
release=`echo $version|cut -d. -f3`
release_number=`echo $version|cut -d. -f3|sed -e 's/\([0-9]*\).*/\1/'`
build=`echo $version|cut -d. -f4`
release_type=$2
edition_name=$3
license_type=$4

echo "----------------------------------------------------"
echo "Setting workbench version"
echo "Version       : $1"
echo "Release Type  : $2"
echo "Edition       : $3"
echo "License       : $4"
echo "Parsed Version: $major.$minor.$release.$build"
echo "----------------------------------------------------"

if test "$build" = ""; then
    build=0
fi

if test "$version" = ""; then
    echo "$0 <major.minor.release[.build]> [<release-type>]"
    exit 1
fi

if test x$release_type != x; then
    dot_release_type=.$release_type
    space_release_type=" $release_type"
else
    dot_release_type=
    space_release_type=
fi

echo "Updating wb_version.h"
sed -e "s/#define APP_MAJOR_NUMBER.*/#define APP_MAJOR_NUMBER $major/" \
    -e "s/#define APP_MINOR_NUMBER.*/#define APP_MINOR_NUMBER $minor/" \
    -e "s/#define APP_RELEASE_NUMBER.*/#define APP_RELEASE_NUMBER $release_number/" \
    -e "s/#define APP_RELEASE_TYPE.*/#define APP_RELEASE_TYPE \"$release_type\"/" \
    -e "s/#define APP_BUILD_NUMBER.*/#define APP_BUILD_NUMBER $build/" \
    -i.bak backend/wbprivate/workbench/wb_version.h 

if test "$edition_name" != "" -a "$license_type" != ""; then
sed -e "s/#define APP_LICENSE_TYPE.*/#define APP_LICENSE_TYPE \"$license_type\"/" \
    -e "s/#define APP_EDITION_NAME.*/#define APP_EDITION_NAME \"$edition_name\"/" \
    -i.bak backend/wbprivate/workbench/wb_version.h 
fi

echo "Updating AssemblyInfo.cs"
sed -e "s/\[assembly: AssemblyVersion(.*)\]/[assembly: AssemblyVersion(\"$major.$minor.$release_number\")]/"\
    -e "s/\[assembly: AssemblyFileVersion(.*)\]/[assembly: AssemblyFileVersion(\"$major.$minor.$release_number\")]/" \
    -e "s/\[assembly: AssemblyReleaseType(.*)\]/[assembly: AssemblyReleaseType(\"$release_type\")]/" \
    -i.bak frontend/windows/MySQLWorkbench/Properties/AssemblyInfo.cs

if test "$edition_type" != ""; then
sed -e "s/\[assembly: AssemblyConfiguration(.*)\]/[assembly: AssemblyConfiguration(\"$edition_type\")]/"\
    -i.bak frontend/windows/MySQLWorkbench/Properties/AssemblyInfo.cs
fi

echo "Updating Info.plist"
sed -e "/<key>CFBundleShortVersionString<\/key>/N;s/.*\n.*<\/string>/	<key>CFBundleShortVersionString<\/key>@	<string>$major.$minor.$release$dot_release_type<\/string>/" -e 'y/@/\n/' -i.bak Info.plist

echo "Updating mysql-workbench.spec.in"
sed -e "s/--define='version.*'/--define='version $major.$minor.$release_number'/" -i.bak build/mysql-workbench.spec.in

# check if build/debian.in/changelog needs update
echo "Checking if build/debian.in/changelog needs updating"
if ! grep $major.$minor.$release- build/debian.in/changelog; then
    echo "build/debian.in/changelog needs update, adding a new entry"
    cat <<EOF > build/debian.in/changelog.new
mysql-workbench-@edition@ ($major.$minor.$release$dot_release_type-1@distrov@) @distro@; urgency=low

  * New release

 -- `git config user.name` (`git config user.email`)  `date +"%a, %d %b %Y %T %z"`

EOF
    cat build/debian.in/changelog >> build/debian.in/changelog.new
    mv -f build/debian.in/changelog.new build/debian.in/changelog
    head -10 build/debian.in/changelog
fi
